package com.amsp.oa.oademo.service.impl;

import com.amsp.oa.oademo.entity.ExternalFile;
import com.amsp.oa.oademo.mapper.ExternalFileMapper;
import com.amsp.oa.oademo.service.ExternalFileService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ExternalFileServiceImpl extends ServiceImpl<ExternalFileMapper, ExternalFile>
        implements ExternalFileService {

    @Override
    public IPage<ExternalFile> getPage(Integer current, Integer size, String keyword) {
        Page<ExternalFile> page = new Page<>(current, size);
        LambdaQueryWrapper<ExternalFile> wrapper = new LambdaQueryWrapper<>();

        if (StringUtils.isNotBlank(keyword)) {
            wrapper.like(ExternalFile::getFileNumber, keyword)
                    .or()
                    .like(ExternalFile::getFileName, keyword)
                    .or()
                    .like(ExternalFile::getProvider, keyword);
        }

        wrapper.orderByDesc(ExternalFile::getCreateTime);
        return this.page(page, wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchSaveOrUpdate(List<ExternalFile> fileList) {
        return this.saveOrUpdateBatch(fileList);
    }
}